package com.firefightingiot_data.data.web.service;

import cn.hutool.json.JSONObject;
import com.firefightingiot_data.data.common.base.BaseService;
import com.firefightingiot_data.data.support.model.RespEntity;
import com.firefightingiot_data.data.support.mybatisplus.QueryWrapper;
import com.firefightingiot_data.data.support.utils.MyLogger;
import com.firefightingiot_data.data.web.mapper.DeviceBusinessMapper;
import com.firefightingiot_data.data.web.mapper.ItemSetupDefaultMapper;
import com.firefightingiot_data.data.web.mapper.ItemSetupMapper;
import com.firefightingiot_data.data.web.model.DeviceBusiness;
import com.firefightingiot_data.data.web.model.ItemSetup;
import com.firefightingiot_data.data.web.model.ItemSetupDefault;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 检测项服务
 */
@Service
public class ItemSetupService extends BaseService {

    @Resource
    public MyLogger logger;

    @Resource
    private ItemSetupMapper itemSetupMapper;
    @Resource
    private ItemSetupDefaultMapper itemSetupDefaultMapper;
    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;

    /**
     * 监测项列表
     * @return
     */
    public RespEntity getItemSetupList(JSONObject jsonParam){

        List<ItemSetup> itemSetupList = itemSetupMapper.getItemSetupList(jsonParam);

        if (itemSetupList.isEmpty()){
            //查询产品id
            DeviceBusiness deviceBusiness = deviceBusinessMapper.selectOne(new QueryWrapper<DeviceBusiness>().eq("number",jsonParam.get("number")));
            if (deviceBusiness!=null){
                //查询检测项默认值
                List<ItemSetupDefault> itemSetupDefaultList = itemSetupDefaultMapper.selectList(new QueryWrapper<ItemSetupDefault>().eq("product_id",deviceBusiness.get("product_id")));
                return RespEntity.success(itemSetupDefaultList);
            }else {
                return RespEntity.success();
            }
        }
        return RespEntity.success(itemSetupList);
    }
}
